package com.py.credit.controller;

import com.py.credit.common.Page;
import com.py.credit.common.logs.ExceptionLogger;
import com.py.credit.common.rest.RestServiceResult;
import com.py.credit.entity.*;
import com.py.credit.service.LhjcHomePageService;
import com.py.credit.service.LhjcRemoteBaseService;
import com.py.credit.service.OfficeService;
import com.py.credit.utils.DateSplitUtil;
import com.py.framework.core.helper.HttpServletHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/lhjcHomePage")
public class LhjcHomePageController {

    @Autowired
    private LhjcHomePageService lhjcHomePageService;

    @Autowired
    private LhjcRemoteBaseService lhjcRemoteBaseService;

    @Autowired
    private OfficeService officeService;

    /**
     * 首页统计
     * @param type T:今天 W:一周 M:月 Z:自定义查询
     * @param checkStartTime 起始时间
     * @param checkEndTime 结束时间
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/allTotalCount.json")
    public RestServiceResult getAllTotalCount(@RequestParam(value = "type", defaultValue = "", required = true) String type,
                                              @RequestParam(value = "checkStartTime", defaultValue = "", required = true) String checkStartTime,
                                              @RequestParam(value = "checkEndTime", defaultValue = "", required = true) String checkEndTime,
                                              HttpServletRequest request) {

        RestServiceResult<SysUser> user = lhjcRemoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));
        SysUser userInfo = null;

        if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
            userInfo = user.getData();
            if (null != userInfo) {

            } else {
                ExceptionLogger.error("LhjcHomePageController#getAllTotalCount", "未获取到当前用户信息！");
                return new RestServiceResult(RestServiceResult.CODE_UNKNOW, "未获取到当前用户信息！");
            }
        } else {
            ExceptionLogger.error("LhjcHomePageController#getAllTotalCount", "未获取到当前用户信息！");
            return new RestServiceResult(RestServiceResult.CODE_UNKNOW, "未获取到当前用户信息！");
        }

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String startTime = "";
        String endTime = "";

        if (type.equals("T")) {
            startTime = sdf.format(DateSplitUtil.getTimesmorning());
            endTime = sdf.format(DateSplitUtil.getTimesnight());
        } else if (type.equals("W")) {
            startTime = sdf.format(DateSplitUtil.getTimesWeekmorning());
            endTime = sdf.format(DateSplitUtil.getTimesWeeknight());
        } else if (type.equals("M")) {
            startTime = sdf.format(DateSplitUtil.getTimesMonthmorning());
            endTime = sdf.format(DateSplitUtil.getTimesMonthnight());
        } else if (type.equals("Z")) {
            startTime = checkStartTime;
            endTime = checkEndTime;
        }

        long cfbm = Long.parseLong(officeService.selectTopDeptId(userInfo.getOfficeId() + ""));

        //本部门发起
        int fqAjAllCount = lhjcHomePageService.getAjbhtListForTotalCount("", cfbm, startTime, endTime);
        int fqAjCjCount = lhjcHomePageService.getAjbhtListForTotalCount("C", cfbm, startTime, endTime);
        int fqAjJlCount = lhjcHomePageService.getAjbhtListForTotalCount("J", cfbm, startTime, endTime);

        //各部门反馈
        int otherFeedbackCount = lhjcHomePageService.getOtherDeptFeedbackListForTotalCount(cfbm, startTime, endTime);

        //本部门参与
        int cyAjAllCount = lhjcHomePageService.getOtherAjbhtListForTotalCount("", cfbm, startTime, endTime);
        int cyAjCjCount = lhjcHomePageService.getOtherAjbhtListForTotalCount("C", cfbm, startTime, endTime);
        int cyAjJlCount = lhjcHomePageService.getOtherAjbhtListForTotalCount("J", cfbm, startTime, endTime);

        //处理反馈
        int cyFeedbackCount = lhjcHomePageService.getFeedbackListForTotalCount(cfbm, startTime, endTime);

        //规则
        int fqGzCount = lhjcHomePageService.selectGzInfoListForTotalCount("", cfbm, startTime, endTime);
        int cyGzCount = lhjcHomePageService.selectOtherGzInfoListForTotalCount("", cfbm, startTime, endTime);

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("fqAjAllCount", fqAjAllCount);
        map.put("fqAjCjCount", fqAjCjCount);
        map.put("fqAjJlCount", fqAjJlCount);
        map.put("otherFeedbackCount", otherFeedbackCount);
        map.put("cyAjAllCount", cyAjAllCount);
        map.put("cyAjCjCount", cyAjCjCount);
        map.put("cyAjJlCount", cyAjJlCount);
        map.put("cyFeedbackCount", cyFeedbackCount);
        map.put("fqGzCount", fqGzCount);
        map.put("cyGzCount", cyGzCount);
        return new RestServiceResult(map);
    }

    /**
     * 图标统计
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/chartCount.json")
    public RestServiceResult getChartCount(HttpServletRequest request) {
        RestServiceResult<SysUser> user = lhjcRemoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));
        SysUser userInfo = null;

        if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
            userInfo = user.getData();
            if (null != userInfo) {

            } else {
                ExceptionLogger.error("LhjcHomePageController#getChartCount", "未获取到当前用户信息！");
                return new RestServiceResult(RestServiceResult.CODE_UNKNOW, "未获取到当前用户信息！");
            }
        } else {
            ExceptionLogger.error("LhjcHomePageController#getChartCount", "未获取到当前用户信息！");
            return new RestServiceResult(RestServiceResult.CODE_UNKNOW, "未获取到当前用户信息！");
        }

        String startTime = "";
        String endTime = "";

        if (null != lhjcHomePageService.selectMaxTime() && null !=lhjcHomePageService.selectMaxTime().get("equal")) {
            if (lhjcHomePageService.selectMaxTime().get("equal").equals("1")) {
                endTime = lhjcHomePageService.selectMaxTime().get("addTime");
            } else {
                endTime = lhjcHomePageService.selectMaxTime().get("sxsj");
            }
        }

        if (null != lhjcHomePageService.selectMinTime() && null != lhjcHomePageService.selectMinTime().get("equal")) {
            if (lhjcHomePageService.selectMinTime().get("equal").equals("0")) {
                startTime = lhjcHomePageService.selectMinTime().get("sxsj");
            } else {
                startTime = lhjcHomePageService.selectMinTime().get("addTime");
            }
        }

        long cfbm = Long.parseLong(officeService.selectTopDeptId(userInfo.getOfficeId() + ""));

        List<ChartNum> fqNum = lhjcHomePageService.selectFqNum(cfbm, startTime, endTime);
        List<ChartNum> cyNum = lhjcHomePageService.selectCyNum(cfbm, startTime, endTime);
        List<ChartNum> xyNum = lhjcHomePageService.selectXyNum(cfbm, startTime, endTime);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("fqNum", fqNum);
        map.put("cyNum", cyNum);
        map.put("xyNum", xyNum);
        map.put("startTime", startTime);
        map.put("endTime", endTime);
        return new RestServiceResult(map);
    }

    /**
     * 点进去列表
     * @param page 分页条件
     * @param index 下标
     * @param date T:今天 W:一周 M:月 Z:自定义查询
     * @param checkStartTime 起始时间
     * @param checkEndTime 结束时间
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/list.json")
    public RestServiceResult getList(Page page,
                                     @RequestParam(value = "index", defaultValue = "", required = true) String index,
                                     @RequestParam(value = "date", defaultValue = "", required = true) String date,
                                     @RequestParam(value = "checkStartTime", defaultValue = "", required = true) String checkStartTime,
                                     @RequestParam(value = "checkEndTime", defaultValue = "", required = true) String checkEndTime,
                                     HttpServletRequest request) {
        RestServiceResult<SysUser> user = lhjcRemoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));
        SysUser userInfo = null;

        if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
            userInfo = user.getData();
            if (null != userInfo) {

            } else {
                ExceptionLogger.error("LhjcHomePageController#getList", "未获取到当前用户信息！");
                return new RestServiceResult(RestServiceResult.CODE_UNKNOW, "未获取到当前用户信息！");
            }
        } else {
            ExceptionLogger.error("LhjcHomePageController#getList", "未获取到当前用户信息！");
            return new RestServiceResult(RestServiceResult.CODE_UNKNOW, "未获取到当前用户信息！");
        }

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String startTime = "";
        String endTime = "";

        if (date.equals("T")) {
            startTime = sdf.format(DateSplitUtil.getTimesmorning());
            endTime = sdf.format(DateSplitUtil.getTimesnight());
        } else if (date.equals("W")) {
            startTime = sdf.format(DateSplitUtil.getTimesWeekmorning());
            endTime = sdf.format(DateSplitUtil.getTimesWeeknight());
        } else if (date.equals("M")) {
            startTime = sdf.format(DateSplitUtil.getTimesMonthmorning());
            endTime = sdf.format(DateSplitUtil.getTimesMonthnight());
        } else if (date.equals("Z")) {
            startTime = checkStartTime;
            endTime = checkEndTime;
        }

        long cfbm = Long.parseLong(officeService.selectTopDeptId(userInfo.getOfficeId() + ""));

        if (index.equals("0")) {
            List<MessInfo> messInfos = lhjcHomePageService.getAjbhtListForPage("", cfbm, startTime, endTime, page);
            return new RestServiceResult(page.getTotalCount(), messInfos);

        } else if (index.equals("2")) {
            List<MessInfo> messInfos = lhjcHomePageService.getAjbhtListForPage("C", cfbm, startTime, endTime, page);
            return new RestServiceResult(page.getTotalCount(), messInfos);

        } else if (index.equals("1")) {
            List<MessInfo> messInfos = lhjcHomePageService.getAjbhtListForPage("J", cfbm, startTime, endTime, page);
            return new RestServiceResult(page.getTotalCount(), messInfos);
        } else if (index.equals("5")) {
            List<MessInfo> messInfos = lhjcHomePageService.getOtherAjbhtListForPage("", cfbm, startTime, endTime, page);
            return new RestServiceResult(page.getTotalCount(), messInfos);
        } else if (index.equals("7")) {
            List<MessInfo> messInfos = lhjcHomePageService.getOtherAjbhtListForPage("C", cfbm, startTime, endTime, page);
            return new RestServiceResult(page.getTotalCount(), messInfos);
        } else if (index.equals("6")) {
            List<MessInfo> messInfos = lhjcHomePageService.getOtherAjbhtListForPage("J", cfbm, startTime, endTime, page);
            return new RestServiceResult(page.getTotalCount(), messInfos);
        } else if (index.equals("3")) {
            List<FeedbackInfo> feedbackInfos = lhjcHomePageService.getOtherDeptFeedbackListForPage(cfbm, startTime, endTime, page);
            return new RestServiceResult(page.getTotalCount(), feedbackInfos);
        } else if (index.equals("8")) {
            List<FeedbackInfo> feedbackInfos = lhjcHomePageService.getFeedbackListForPage(cfbm, startTime, endTime, page);
            return new RestServiceResult(page.getTotalCount(), feedbackInfos);
        } else if (index.equals("4")) {
            List<GzInfo> gzInfos = lhjcHomePageService.selectGzInfoListForPage("", cfbm, startTime, endTime, page);
            return new RestServiceResult(page.getTotalCount(), gzInfos);
        } else {
            List<GzInfo> gzInfos = lhjcHomePageService.selectOtherGzInfoListForPage("", cfbm, startTime, endTime, page);
            return new RestServiceResult(page.getTotalCount(), gzInfos);
        }
    }

    /**
     * Excel统计导出
     * @param checkStartTime 起始时间
     * @param checkEndTime 结束时间
     * @param request
     * @param response
     * @return
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping(value = "/exportExcel.json")
    public RestServiceResult excelExport(@RequestParam(value = "checkStartTime", defaultValue = "", required = true) String checkStartTime,
                                         @RequestParam(value = "checkEndTime", defaultValue = "", required = true) String checkEndTime,
                                         HttpServletRequest request,
                                         HttpServletResponse response) throws IOException {
        RestServiceResult<SysUser> user = lhjcRemoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));
        SysUser userInfo = null;

        if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
            userInfo = user.getData();
            if (null != userInfo) {

            } else {
                ExceptionLogger.error("LhjcHomePageController#excelExport", "未获取到当前用户信息！");
                return new RestServiceResult(RestServiceResult.CODE_UNKNOW, "未获取到当前用户信息！");
            }
        } else {
            ExceptionLogger.error("LhjcHomePageController#excelExport", "未获取到当前用户信息！");
            return new RestServiceResult(RestServiceResult.CODE_UNKNOW, "未获取到当前用户信息！");
        }

        lhjcHomePageService.excelExport(response, Long.parseLong(officeService.selectTopDeptId(userInfo.getOfficeId() + "")), checkStartTime, checkEndTime);
        return new RestServiceResult(RestServiceResult.CODE_SUCCESS, "导出成功");
    }
}
